angular.module('webapp') //找打主模块
	.service("NewsService",["$http","$q",NewsService]); //依赖注入


function NewsService($http,$q){

	//处理通用的请求
	function handleRequest(method,url,data){
		var defered = $q.defer();
		var config = {
			method:method,
			url:url
		};

		if("POST" === method){
			config.data = data;
		}else if("GET" === method){
			config.params = data;
		}

		//执行http请求，将结果返回给$q
		$http(config).then(function(data){
			defered.resolve(data);
		}).catch(function(err){
			defered.reject(err);
		});

		return defered.promise;
	}

	return {
		list:function(params){
			return handleRequest('GET','/news',params);
		},
		save:function(data){
			return handleRequest('POST','/news',data);
		},
		detail:function(id){
			return handleRequest('GET','/news/'+id);
		}
	}
}
